;
(function() {
  'use strict';

  angular.module('controllers.userManager.user')
    .controller('roleManagerCtrl', roleManagerCtrl);
  /**
   * [userSearchCtrl description]
   * @param  {[type]} $scope      [Angular scope]
   * @param  {[type]} userRequest [Request server]
   * @param  {[type]} userCRUD    [service]
   * @param  {[type]} getIds      [checkbox]
   */
  function roleManagerCtrl($scope, roleManagerServer, CoreService, $mdDialog, roleManagerFormServer) {
    $scope.role = roleManagerFormServer.role;
    $scope.querters = [{
      querterName: '查询所有',
      querterCode: '00'
    }, {
      querterName: '角色名称',
      querterCode: '01'
    }, {
      querterName: '角色编码',
      querterCode: '04'
    }, {
      querterName: '创建时间',
      querterCode: '02'
    }, /*{
      querterName: '通过上级角色查询',
      querterCode: '03'
    } */];
    $scope.querterCondition = '00';
    $scope.roles = [{
      roleName: '系统管理员',
      roleCode: '1'
    }, {
      roleName: '财务管理员',
      roleCode: '2'
    }, {
      roleName: '维护管理员',
      roleCode: '3'
    }, {
      roleName: '管理员',
      roleCode: '4'
    }];

    $scope.$watch('querterCondition', function(value){
      $scope.role.roleName='',
      $scope.role.roleCode= '',
      $scope.role.creTime= '',
      $scope.role.parentName= '';
    });
    queryRole()
    $scope.shoumun = 0;
    $scope.queryAllChannel = function() {
      $scope.shoumun == 1 ? $scope.shoumun = 0 : $scope.shoumun = 1;
      if ($scope.shoumun === 0) {
        queryRole();
      }
    }

    function queryRole() {
      clearData();
      roleManagerServer.querRoleManger(function(result) {
        if (result.state === '10000') {
          result.data.length > 0 ? $scope.items = result.data : CoreService.dialog(result.state);
        } else {
          CoreService.dialog(result.state);
        }
      });
    }
    /**
     * 检查选择
     */
    var updata = [];
    var roleCode = [];
    $scope.data = {
      roleIds: []
    };
    $scope.check = function(roles) {
      var index = $scope.data.roleIds.indexOf(roles.roleId);
      if (index === -1) {
        $scope.data.roleIds.push(roles.roleId);
        updata.push(roles);
        roleCode.push(roles.roleCode);
      } else {
        $scope.data.roleIds.splice(index, 1);
        updata.splice(index, 1);
        roleCode.splice(index, 1);
      }
    };
    /**
     *  清空数组的方法
     *  用于查询修改,删除
     */
    function clearData() {
      updata = [];
      roleCode = [];
      $scope.data = {
        roleIds: []
      };
    }
    /**
     *  使用material 弹出框
     *  添加角色的方法定义数组roleAdd,在服务中创建get()set()方法
     *  把当前角色id和下拉框数据传入到添加页面,使得上级角色默认选择当前角色
     *  
     *  给弹出框页面一个控制器,制订弹出框页面位置
     */
    $scope.roleAdd = function(ev) {
      var roleAdd = [];
      $mdDialog.show({
        controller: addController,
        templateUrl: 'views/sysMng/roleMng/RMUpdataAddTemplate.html',
        targetEvent: ev,
      }).then(function(answer) {
        queryRole();
      }, function() {
        queryRole();
      });
    };
    /**
     *  删除说选择的角色
     */
    function roleDelete() {
      var params = {
        roleIds: ''
      };
      var param = $scope.data.roleIds;
      for (var i = 0; i < param.length - 1; i++) {
        params.roleIds += param[i] + ',';
      }
      params.roleIds += param[param.length - 1];
      roleManagerServer.deleteRole(params, function(result) {
        if (result.state === '10000') {
          queryRole();
        } else {
          var testParams = [];
          testParams.push(result.roleId);
          caveat('无法删除');
          queryRole();
        }
      });
    }
    /**
     *  提示方法 用于删除提示确定是否上场
     */
    $scope.showConfirm = function() {
      roleDelete();
    };

    /**
     *  提示方法:用于保存修改提示传入提示信息
     */
    function caveat(string) {
      $mdDialog.show(
        $mdDialog.alert()
        .title('提示框')
        .content(string)
        .ariaLabel('Password notification')
        .ok('确定')
      );
    }
    /*
     *  增加角色控制器
     *  接受服务传入数据（下拉框数据,当前角色id）
     *  给页面添加提示属性 att用于弹出框的显示添加属性
     */
    function addController($scope, $mdDialog) {
      $scope.att = '添加';
      $scope.role = {};
      $scope.parentRole = '4';
      $scope.roles = [{
          roleName: '系统管理员',
          roleCode: '1'
        }, {
          roleName: '财务管理员',
          roleCode: '2'
        }, {
          roleName: '维护管理员',
          roleCode: '3'
        }, {
          roleName: '管理员',
          roleCode: '4'
        }]
        /**
         *  取消弹出框方法
         */
      $scope.hide = function() {
        $mdDialog.hide();
      };
      /**
       * 保存添加方法
       *
       * 保存成功：取消弹出框,调用方法查询数据;调用提示方法,给用户提示
       */
      $scope.save = function() {
        var role = {};
        role.roleCode = $scope.roleCode;
        role.roleName = $scope.roleName;
        role.parentRole = $scope.parentRole;
        role.parentName = $scope.parentRole === '1' ? '系统管理员' : $scope.parentRole === '2' ? '财务管理员' : $scope.parentRole === '3' ? '维护管理员' : '管理员'
        role.roleDetail = $scope.roleDetail;
        roleManagerServer.addRole(role, function(result) {
          if (result.state === '10000') {
            $mdDialog.hide();
          } else {
            caveat(role.roleCode + result.message + '保存失败');
          }
        });
      };

    }
    /**
     *  使用material 弹出框
     *  取得,在服务中创建get()set()方法
     *  把需要修改角色数据存入服务中
     *  
     *  给弹出框页面一个控制器,制订弹出框页面位置
     */
    $scope.roleUpdate = function(ev) {
      roleManagerServer.setMessage(updata);
      $mdDialog.show({
        controller: updateController,
        templateUrl: 'views/sysMng/roleMng/RMUpdataAddTemplate.html',
        targetEvent: ev,
      }).then(function(answer) {
        queryRole();
      }, function() {
        queryRole();
      });
    };

    function updateController($scope, $mdDialog) {
      $scope.roles = [{
        roleName: '系统管理员',
        roleCode: '1'
      }, {
        roleName: '财务管理员',
        roleCode: '2'
      }, {
        roleName: '维护管理员',
        roleCode: '3'
      }, {
        roleName: '管理员',
        roleCode: '4'
      }]
      $scope.att = '修改';
      $scope.roleDetail = roleManagerServer.getMessage()[0].roleDetail;
      $scope.roleCode = roleManagerServer.getMessage()[0].roleCode;
      $scope.roleName = roleManagerServer.getMessage()[0].roleName;
      $scope.parentRole = roleManagerServer.getMessage()[0].parentRole;

      /**
       *  取消弹出框方法
       */
      $scope.hide = function() {
        $mdDialog.hide();
      };

      /**
       * 保存修改方法
       *
       * 修改成功：取消弹出框,调用方法查询数据;调用提示方法,给用户提示
       *
       * 修改失败： 给用户提示,不取消弹出框;给用户提示信息
       */
      $scope.updata = function() {
        var role = {};
        role.roleCode = $scope.roleCode;
        role.roleName = $scope.roleName;
        role.parentRole = $scope.parentRole;
        role.parentName = $scope.parentRole === '1' ? '系统管理员' : $scope.parentRole === '2' ? '财务管理员' : $scope.parentRole === '3' ? '维护管理员' : '管理员'
        role.roleDetail = $scope.roleDetail;
        role.roleId = roleManagerServer.getMessage()[0].roleId;
        roleManagerServer.modify(role, function(result) {
          if (result.state === '10000') {
            $mdDialog.hide();
          } else {
            caveat(role.roleCode + result.message + '修改失败');
          }
        });
      };
    }

  }
})();